﻿$(function () {

    $('#flexnav').flexNav();

    $('#flexCategory li').mouseenter(function () {
        var ul = $(this).find('ul:first');
        if (!$(ul).is(":visible")) {
            $(ul).show();
        }
    }).mouseleave(function () {
        $(this).find('ul:first').hide();       
    });

    $('.carousel').carousel({
        interval: 6000
    });

    $(".coupon-title").shorten({
        width: 150
    });

    $(".coupon-desciption").shorten({
        width: 70    
    });

    $("#btnBlogWrite").click(function () {
        var languageid = $(this).attr("languageid");
        window.location.href = "/blog/" + languageid + "/new";
    });

    $("#btnNewsletter").click(function () {
        var email = document.getElementById("txtNewsletter").value;

        if (email == '') {
            document.getElementById("txtNewsletter").focus();
            return;
        }

        var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,4}$/;

        if(pattern.test(email)){          
       
            var params = {
                'action': "regNewsletter",
                'email': email
            };

            $.ajax({
                type: "GET",
                url: "/Tools/Submit_Ajax.ashx",
                data: params,
                dataType: 'html',
                timeout: 5000,
                success: function (data) {
                    if (parseInt(data) == -100) {
                        alert("Email đã được dùng đăng ký !");
                        return;
                    }
                    else if (parseInt(data) <= 0) {
                        alert("Lỗi xảy ra, vui lòng thử lại !");
                        return;
                    }
                    else {
                        document.getElementById("txtNewsletter").value = '';
                        document.getElementById("txtNewsletter").focus();
                        alert("Đăng ký thành công !");
                        return;
                    }
                }
            });
        }
    });

});


// Bootstrap tooltips
$('[data-toggle="tooltip"]').tooltip();


// Bootstrap accordion
$('.accordion').on('show', function (e) {
    $(e.target).prev('.accordion-heading').find('.accordion-toggle').addClass('active');
});

$('.accordion').on('hide', function (e) {
    $(e.target).prev('.accordion-heading').find('.accordion-toggle').removeClass('active');
});

$("#accordion-toggle .collapse").collapse();



$(document).ready(function () {
    // Sticky navigation
    if ($('body').hasClass('sticky-header')) {
        var theLoc = $('header.main').position().top;
        $(window).scroll(function () {
            if (theLoc >= $(window).scrollTop()) {
                if ($('header.main').hasClass('fixed')) {
                    $('header.main').removeClass('fixed');
                }
            } else {
                if (!$('header.main').hasClass('fixed')) {
                    $('header.main').addClass('fixed');
                }
            }
        });
    }

    // Slider
    $("#mainslider").flexslider({
        animation: "slide",
        slideshow: true, // ***
        useCSS: false,
        controlNav: false,
        animationLoop: true,
        smoothHeight: false
    });
       

});


function showModal(msg, err, id) {

    $(".modal-body").html(msg);

    if (document.getElementById("hdErrCode")) {
        document.getElementById("hdErrCode").value = err;
    }

    if (arguments.length == 3) {
        $(id).modal({
            keyboard: false,
            backdrop: true,
            show: true
        });
    }
    else {
        $("#modalResult").modal({
            keyboard: false,
            backdrop: true,
            show: true
        });
    }
}
